package drone.iqgc.util ;

import java.math.BigDecimal ;
import java.util.ArrayList ;
import java.util.List ;

import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;

import drone.basis.util.StringUtil ;
import drone.facade.model.entity.auth.AuthDepartmentEntity ;
import io.reactivex.Observable ;
import io.reactivex.functions.Consumer ;
import io.reactivex.functions.Function ;

public class Rxjava
{

	private static final Logger logger = LoggerFactory.getLogger ( Rxjava.class ) ;

	public static List < AuthDepartmentEntity > init ( )
	{
		
		logger.info ( "init" );
		List < AuthDepartmentEntity > list = new ArrayList < AuthDepartmentEntity > ( ) ;

		AuthDepartmentEntity d1 = new AuthDepartmentEntity ( ) ;
		d1.setDptNo ( StringUtil.guid ( ) ) ;
		d1.setDptName ( "张三" ) ;
		d1.setDptLevel ( "4,5,87" );
		d1.setDptSort ( new BigDecimal ( 1 ) ) ;

		list.add ( d1 ) ;

		AuthDepartmentEntity d2 = new AuthDepartmentEntity ( ) ;
		d2.setDptNo ( StringUtil.guid ( ) ) ;
		d2.setDptName ( "李四" ) ;
		d2.setDptLevel ( "76,1,0" );
		d2.setDptSort ( new BigDecimal ( 2 ) ) ;

		list.add ( d2 ) ;

		AuthDepartmentEntity d3 = new AuthDepartmentEntity ( ) ;
		d3.setDptNo ( StringUtil.guid ( ) ) ;
		d3.setDptName ( "王五" ) ;
		d3.setDptLevel ( "9,66,43" );
		d3.setDptSort ( new BigDecimal ( 3 ) ) ;

		list.add ( d3 ) ;

		return list ;

	}
	
	public static void testMap() {
		Observable.fromIterable ( init() ).map ( new Function < AuthDepartmentEntity , List<String> >( )
		{

			@Override
			public List<String>  apply (
					AuthDepartmentEntity t
			) throws Exception
			{
				// TODO Auto-generated method stub
				List<String> s=StringUtil.split ( t.getDptLevel ( ) );
				
				return s ;
			}
		} ).subscribe ( new Consumer < List<String> >( )
		{

			@Override
			public void accept (
					List < String > t
			) throws Exception
			{
				
				System.out.println ( "===========" ) ;
				for(String item:t){
					System.out.println ( item ) ;
					
				}
				System.out.println ( "===========" ) ;
				
			}
		});
	}
	
	public static void testFlatMap() {
		
		Observable.fromIterable ( init() ).flatMap ( obj -> Observable.fromIterable (StringUtil.split ( obj.getDptLevel ( ) )) )
		
		.subscribe ( con -> System.out.println ( con ));
		
	}
	
	public static void testConcatMap() {
		Observable.fromIterable ( init() ).concatMap ( new Function < AuthDepartmentEntity ,Observable < String > >( )
		{

			@Override
			public Observable < String > apply (
					AuthDepartmentEntity t
			) throws Exception
			{
				// TODO Auto-generated method stub
				
				List<String> s=StringUtil.split ( t.getDptLevel ( ) );
				
				return Observable.fromIterable ( s ) ;
			}

			
		} ).subscribe ( new Consumer < String >( )
		{

			@Override
			public void accept (
					String t
			) throws Exception
			{
				// TODO Auto-generated method stub
				System.out.println ( "===========" ) ;
					System.out.println ( t ) ;
					
				System.out.println ( "===========" ) ;
			}
			
		});
	}

	public static void main (
			String [ ] args
	)
	{
		testFlatMap ( );
		
	}
}
